package com.dongchedi.controller;

import com.alibaba.fastjson.JSONArray;
import com.dongchedi.service.ProductCategoryService;
import com.dongchedi.utils.ResultMessage;
import com.dongchedi.utils.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author leibidong
 * @description   商品分类controller层
 * @date 2021/6/24
 */

@Controller
@RequestMapping("api")
@ResponseBody
public class ProductCategoryController {
    @Autowired
    ProductCategoryService productCategoryService;
    /*
     * @Author: leibidong
     * @Description: 根据一级类目查询前六件商品
     * @Date: 2021/6/26 19:55
     * @Param: [name, firstCategoryLevel]
     * @Return: java.lang.String
     **/
    @GetMapping("/select/first/classify")
    public String firstQuery(String name,Integer firstCategoryLevel){
        JSONArray jsonArray=new JSONArray();
        String s;
        if(firstCategoryLevel==1) {
            jsonArray = productCategoryService.firstCategory(name, firstCategoryLevel);
            s=ResultMessage.getJSONArrayString(1,"成功",jsonArray);
        }else{
            s=ResultMessage.getJSONArrayString(0,"失败",jsonArray);
        }
        return s;
    }
    /*
     * @Author: leibidong
     * @Description: 根据二级类目查询商品
     * @Date: 2021/6/26 20:17
     * @Param: [name, secondCategoryLevel, pageNum, pageSize]
     * @Return: java.lang.String
     **/
    @GetMapping("/select/second/classify")
    public String secondQuery(String name,Integer secondCategoryLevel,Integer pageNum,Integer pageSize){
        String s;
        JSONArray jsonArray = new JSONArray();
        if(secondCategoryLevel==2) {
            PageRequest pageRequest = new PageRequest(pageNum, pageSize);
            jsonArray=productCategoryService.secondCategory(pageRequest,name,secondCategoryLevel);
            s=ResultMessage.getJSONArrayString(1,"成功",jsonArray);
        }else{
            s=ResultMessage.getJSONArrayString(0,"失败",jsonArray);
        }
        return s;
    }
    /*
     * @Author: leibidong
     * @Description: 根据一级类目名称查询所有二级类目名称
     * @Date: 2021/6/26 20:17
     * @Param: [name]
     * @Return: java.lang.String
     **/
    @GetMapping("/select/categoryname")
    public String categorynameSearch(String name){
        JSONArray jsonArray=new JSONArray();
        String s;
        if(!name.equals("")) {
            jsonArray = productCategoryService.firstSearchSecondName(name);
            s=ResultMessage.getJSONArrayString(1,"成功",jsonArray);
        }else{
            s=ResultMessage.getJSONArrayString(0,"失败",jsonArray);
        }
        return s;
    }
}
